<?php
/**
 * User list administer
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       Zend_Controller_Action
 * @package    User
 * @subpackage Controller
 * @todo
 */
class User_UserlistController extends Zend_Controller_Action
{
    /**
     * Initialisation
     *
     * @return void
     */
    public function init()
    {
        $this->view->headLink()->appendStylesheet($this->view->baseUrl("/css/user/app_user.css"));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/user/userlist.js'));

        $userId = $this->_request->getParam("userid");

        if (empty($userId)) {
            $identity = Zend_Auth::getInstance()->getIdentity();
            $userId = $identity['id'];
        }

        $user = new User_Model_User();
        $this->view->userList = array('userList' => $user->getList(), "user" => $user->read($userId));
        $this->view->userId = $userId;
    }

    /**
     * Show user list
     *
     * @return void
     */
    public function indexAction()
    {
        
        $user = new User_Model_User();
        $this->view->user = $user->read($this->view->userId);
    }

    /*
     * Edit user
     * 
     * @return void
     */
    public function editAction()
    {
        $user = new User_Model_User();
        $formRequest = $this->_request->getPost('form');

        if($formRequest) {

            $input = User_Filter_Form::edit($formRequest);

            if($input->isValid()) {
                $user->save($input->getEscaped(), $this->view->userId);
                $this->_forward('index', null, null, array('userid' => $this->view->userId));
                return;
            } else {
                $this->view->messages = $input->getMessages();
                $errors = $input->getErrors();

                /** Set error messages for the view */
                foreach ($errors as $fieldname => $errorTyp) {

                    $this->view->{$fieldname . 'Error'} = 'error';
                }
                $data = $formRequest;
            }
        } else {
            $data = $user->read($this->view->userId);
        }
        $role = new User_Model_Role();
        $this->view->roleList = $role->getList();

        $this->view->firstname = $data['firstname'];
        $this->view->lastname  = $data['lastname'];
        $this->view->username  = $data['username'];
        $this->view->role      = $data['role'];
    }
    
    /*
     * Create user
     * 
     * @return void
     */
    public function createAction()
    {
        $this->view->userList["user"]["id"] = null;
        $user = new User_Model_User();
        $formRequest = $this->_request->getPost('form');

        if($formRequest) {

            if($formRequest["password"] != $formRequest["repeat"]) {

                $this->view->messages = array("password" => array("Passwörter stimmen nicht überein"));
                $this->view->passwordError = 'error';
                $this->view->repeatError = 'error';
            } else {

                $input = User_Filter_Form::create($formRequest);

                if($input->isValid()) {
                    $userId = $user->save($input->getEscaped());
                    $this->_forward('index', null, null, array('userid' => $userId));
                    return;
                } else {
                    $this->view->messages = $input->getMessages();
                    $errors = $input->getErrors();

                    /** Set error messages for the view */
                    foreach ($errors as $fieldname => $errorTyp) {

                        $this->view->{$fieldname . 'Error'} = 'error';
                    }
                }
            }
        }
        $role = new User_Model_Role();
        $this->view->roleList = $role->getList();

        $data = $formRequest;

        $this->view->firstname = $data['firstname'];
        $this->view->lastname  = $data['lastname'];
        $this->view->username  = $data['username'];
        $this->view->role      = $data['role'];
    }

    /*
     * Delete user
     * 
     * @return void
     */
    public function deleteAction()
    {
        $identity = Zend_Auth::getInstance()->getIdentity();

        if ($identity['id'] != $this->view->userId) {

            $user = new User_Model_User();
            $user->delete($this->view->userId);
        } else {
            $this->view->alertError = "Der Benutzer darf nicht gelöscht werden!";
        }
        $user = new User_Model_User();
        $this->view->userList = array('userList' => $user->getList(), 'userId' => null);
    }
    
    /**
     * Change password
     */
    public function passwordAction()
    {
        $formRequest = $this->_request->getPost('form');

        if($formRequest) {

            $input = User_Filter_Form::password($formRequest);

            if($input->isValid()) {

                $password = new User_Model_Password();
                if($formRequest["new"] == $formRequest["repeat"]) {

                    $identity = Zend_Auth::getInstance()->getIdentity();
                    $password->change($formRequest['new'], $this->view->userId);
                    $this->view->alertSuccess = "Passwort wurde geändert";

                } else {
                    $this->view->alertError = "Passwörter stimmen nicht überein";
                    $this->view->newError = 'error';
                    $this->view->repeatError = 'error';
                }
            } else {

                $this->view->alertError = "Mindestlänge 8 Zeichen";
            }
        }
    }
}